<?
include(dirname(__FILE__)."/../"."global.php");

include(INCLUDE_PATH . "/common/common.func.php");

require(INCLUDE_PATH . "/common/db_mysql.class.php");

$objDB		= new db_mysql(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if(!file_exists(PROJECT_ROOT."/cache/town")){
	mkdir(PROJECT_ROOT."/cache/town");
}

//------------------------------------[cache_npc_lvids.php]---------------------
$strSql = "SELECT * FROM town_npc_lvids_conf ORDER BY layer";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$arrData[$objDB->record['layer']]  = $objDB->record;
}
$filename    = PROJECT_ROOT."/cache/town/cache_npc_lvids.php";
$content = '<?php'."\n";
$content .= '$town_npc_lvids = array();'."\n";
foreach($arrData as $id => $arrTmp) {
	$content .= '$town_npc_lvids['.$id.']=array(';
	foreach($arrTmp as $key=>$val){
		if('layer'!=$key){
			$content .= '"'.$key.'"=>"'.$val.'",';
		}
	}
	$content .= ");\n";
}
$content .= "?>";
$fp = fopen($filename,"w");
fwrite($fp,$content);
fclose($fp);
//------------------------------------[cache_npc_base.php]---------------------
$strSql = "SELECT * FROM town_npc_base_conf ORDER BY level";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$arrData[$objDB->record['level']]  = $objDB->record;
}
$filename    = PROJECT_ROOT."/cache/town/cache_npc_base.php";
$content = '<?php'."\n";
$content .= '$town_npc_base = array();'."\n";
foreach($arrData as $id => $arrTmp) {
	$content .= '$town_npc_base['.$id.']=array(';
	foreach($arrTmp as $key=>$val){
		if($key != 'level'){
			$content .= '"'.$key.'"=>"'.$val.'",';
		}
	}
	$content .= ");\n";
}
$content .= "?>";
$fp = fopen($filename,"w");
fwrite($fp,$content);
fclose($fp);
//------------------------------------[cache_npc_quality.php]---------------------
$strSql = "SELECT * FROM town_npc_quality_conf ORDER BY quality";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$arrData[$objDB->record['quality']]  = $objDB->record;
}
$filename    = PROJECT_ROOT."/cache/town/cache_npc_quality.php";
$content = '<?php'."\n";
$content .= '$town_npc_quality = array();'."\n";
foreach($arrData as $id => $arrTmp) {
	$content .= '$town_npc_quality['.$id.']=array(';
	foreach($arrTmp as $key=>$val){
		if($key != 'quality'){
			$content .= '"'.$key.'"=>"'.$val.'",';
		}
	}
	$content .= ");\n";
}
$content .= "?>";
$fp = fopen($filename,"w");
fwrite($fp,$content);
fclose($fp);
//------------------------------------[cache_npc_type.php]---------------------
$strSql = "SELECT * FROM town_npc_type_conf ORDER BY type";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$arrData[$objDB->record['type']][$objDB->record['stype']] = $objDB->record;
}
$filename    = PROJECT_ROOT."/cache/town/cache_npc_type.php";
$content = '<?php'."\n";
$content .= '$town_npc_type = array();'."\n";
foreach($arrData as $id => $arrTmp){
	foreach($arrTmp as $nextId=> $arrTemp){
		$content .= '$town_npc_type['.$id.']['.$nextId.']=array(';
		$content .= ");\n";
	}
}
$content .= "?>";
$fp = fopen($filename,"w");
fwrite($fp,$content);
fclose($fp);
//------------------------------------[cache_npc_attr.php]---------------------
$strSql = "SELECT * FROM town_npc_attr_conf ORDER BY stype";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$arrData[$objDB->record['stype']] = $objDB->record;
}
$filename    = PROJECT_ROOT."/cache/town/cache_npc_attr.php";
$content = '<?php'."\n";
$content .= '$town_npc_attr = array();'."\n";
foreach($arrData as $id => $arrTmp) {
	$content .= '$town_npc_attr['.$id.']=array(';
	foreach($arrTmp as $key=>$val){
		if('stype' != $key){
			$content .= '"'.$key.'"=>"'.$val.'",';
		}
	}
	$content .= ");\n";
}
$content .= "?>";
$fp = fopen($filename,"w");
fwrite($fp,$content);
fclose($fp);
//------------------------------------[cache_layer_config.php]---------------------
$strSql = "SELECT * FROM town_layer_config ORDER BY layer";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$arrData[$objDB->record['layer']] = $objDB->record;
}
$filename    = PROJECT_ROOT."/cache/town/cache_layer_config.php";
$content = '<?php'."\n";
$content .= '$town_layer_config = array();'."\n";
foreach($arrData as $id => $arrTmp) {
	$content .= '$town_layer_config['.$id.']=array(';
	foreach($arrTmp as $key=>$val){
		if($key != 'layer'){
			$content .= '"'.$key.'"=>"'.$val.'",';
		}
	}
	$content .= ");\n";
}
$content .= "?>";
$fp = fopen($filename,"w");
fwrite($fp,$content);
fclose($fp);
//------------------------------------[cache_boss_conf.php]---------------------
$strSql = "SELECT * FROM town_boss_config ORDER BY layer";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$arrData[$objDB->record['layer']] = $objDB->record;
}
$filename    = PROJECT_ROOT."/cache/town/cache_boss_conf.php";
$content = '<?php'."\n";
$content .= '$town_boss = array();'."\n";
foreach($arrData as $id => $arrTmp) {
	$content .= '$town_boss['.$id.']=array(';
	foreach($arrTmp as $key=>$val){
		if('layer' != $key){
			$content .= '"'.$key.'"=>"'.$val.'",';
		}
	}
	$content .= ");\n";
}
$content .= "?>";
$fp = fopen($filename,"w");
fwrite($fp,$content);
fclose($fp);
//------------------------------------[cache_boss_hide.php]---------------------
$strSql = "SELECT * FROM town_hide_boss_conf ORDER BY layer";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$arrData[$objDB->record['layer']] = $objDB->record;
}
$filename    = PROJECT_ROOT."/cache/town/cache_boss_hide.php";
$content = '<?php'."\n";
$content .= '$town_boss_hide = array();'."\n";
foreach($arrData as $layer => $arrTmp) {
	$content .= '$town_boss_hide['.$layer.'] = array("nboss" => "'.$arrTmp["nboss"].'");'."\n";
}
$content .= "?>";
$fp = fopen($filename,"w");
fwrite($fp,$content);
fclose($fp);
//------------------------------------[cache_town_img_factor.php]---------------------
$strSql = "SELECT * FROM town_img_factor_conf ORDER BY layer";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$arrData[$objDB->record['layer']] = $objDB->record;
}
$filename    = PROJECT_ROOT."/cache/town/cache_town_img_factor.php";
$content = '<?php'."\n";
$content .= '$town_img_factor = array();'."\n";
foreach($arrData as $layer => $arrTmp) {
	$content .= '$town_img_factor['.$layer.'] = array("life" => "'.$arrTmp["life"].'","magic" => "'.$arrTmp["magic"].'","attack" => "'.$arrTmp["attack"].'","defence" => "'.$arrTmp["defence"].'","speed" => "'.$arrTmp["speed"].'");'."\n";
}
$content .= "?>";
$fp = fopen($filename,"w");
fwrite($fp,$content);
fclose($fp);
//------------------------------------[npc_attr_addition_file.php]---------------------
$strSql = "SELECT MAX(level) lv FROM town_npclv_addition_conf";
$objDB->query($strSql);
$levelMax = 1;
if($objDB->nextrecord()){
	$levelMax = $objDB->record['lv'];
}

$strSql = "SELECT * FROM town_npclv_addition_conf ORDER BY idx_type";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$idxType = $objDB->record['idx_type'];
	$level   = $objDB->record['level'];
	$arrData[$levelMax*($idxType-1)+$level] = $objDB->record;
}
$fileName    = PROJECT_ROOT."/cache/town/npc_attr_addition_file.cache";
$cacheData = "";
$cacheData .=pack('II',73,$levelMax);
foreach($arrData as $id=>$temp){
	$cacheData .= "\n";
	$cacheData .= pack('a36a36',$id,$temp['addition']);
}
$cacheData .= "\n";
$fp = fopen($fileName,"w");
fwrite($fp,$cacheData);
fclose($fp);

//------------------------------------[cache_npc_skill.php]---------------------
$strSql = "SELECT MAX(factor) lv FROM town_npc_skill_conf";
$objDB->query($strSql);
$levelMax = 1;
if($objDB->nextrecord()){
	$levelMax = $objDB->record['lv'];
}

$strSql = "SELECT * FROM town_npc_skill_conf ORDER BY idx";
$objDB->query($strSql);
$arrData = array();
while($objDB->nextrecord()){
	$idxType = $objDB->record['idx'];
	$level   = $objDB->record['factor'];
	$arrData[$levelMax*($idxType-1)+$level] = $objDB->record;
}
$fileName    = PROJECT_ROOT."/cache/town/npc_skill_file.cache";
$cacheData = "";
$cacheData .=pack('II',73,$levelMax);
foreach($arrData as $id=>$temp){
	$cacheData .= "\n";
	$cacheData .= pack('a36a36',$id,$temp['skill_id']);
}
$cacheData .= "\n";
$fp = fopen($fileName,"w");
fwrite($fp,$cacheData);
fclose($fp);
//------------------------------------[close DB]-----------------------------------------------
$objDB->close();
unset($objDB);
?>